package com.boot.project.admin.controller.manage;

import com.boot.project.core.model.system.entity.Resource;
import com.boot.project.core.model.system.entity.Role;
import com.boot.project.core.model.system.entity.User;
import com.boot.project.module.system.service.ResourceService;
import com.boot.project.module.system.service.RoleService;
import com.boot.project.module.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 * @description: 模态框数据加载控制器
 * @author: tsing
 * @create: 2021-03-04 21:56
 */
@Controller
@RequestMapping("/load")
public class LoadController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private ResourceService resourceService;

    /**
     * 新增时加载保存用户的模态框
     * @return
     */
    @GetMapping("/user")
    public String user() {
        return "/user/form";
    }

    /**
     * 更新时加载保存用户的模态框
     * @param model 模型对象
     * @param id 用户id
     * @return
     */
    @GetMapping("/user/{id}")
    public String user(Model model, @PathVariable("id") Long id) {
        User user = userService.findOne(id);
        model.addAttribute("user", user);
        return "/user/form";
    }

    /**
     * 分配角色时加载分配角色的模态框
     * @param model 模型对象
     * @param id 用户id
     * @return
     */
    @GetMapping("/user/assign/{id}")
    public String assign(Model model, @PathVariable("id") Long id) {
        List<Role> roles = roleService.findAll();
        Role dbRole = roleService.findByUserId(id);
        model.addAttribute("roles", roles);
        model.addAttribute("checked", dbRole == null ? null : dbRole.getId());
        return "/user/assign";
    }

    /**
     * 新增时加载保存角色的模态框
     * @return
     */
    @GetMapping("/role")
    public String role() {
        return "/role/form";
    }

    /**
     * 更新时加载保存角色的模态框
     * @param model 模型对象
     * @param id 角色id
     * @return
     */
    @GetMapping("/role/{id}")
    public String role(Model model, @PathVariable("id") Long id) {
        Role role = roleService.findOne(id);
        model.addAttribute("role", role);
        return "/role/form";
    }

    /**
     * 新增时加载保存资源的模态框
     * @return
     */
    @GetMapping("/resource")
    public String resource(Model model) {
        Resource resource = new Resource();
        resource.setType(1);
        model.addAttribute("resource", resource);
        return "/resource/form";
    }

    /**
     * 更新时加载保存资源的模态框
     * @param model 模型对象
     * @param id 资源id
     * @return
     */
    @GetMapping("/resource/{id}")
    public String resource(Model model, @PathVariable("id") Long id) {
        Map<String, Object> map = resourceService.findForLoadEdit(id);
        model.addAttribute("resource", map.get("resource"));
        model.addAttribute("parentName", map.get("parentName"));
        model.addAttribute("parentType", map.get("parentType"));
        return "/resource/form";
    }
}
